package com.ybkj.daijia.server.websocket;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONObject;
import org.springframework.web.socket.TextMessage;

public class WebSocketSend {

    /**
     * 广播给所有登录的管理员
     *
     * @param type
     * @param data
     * @throws IOException
     */
    public static void boardcast(String type, Object data) {

        try {
            Collection<WebSocketSessionEx> sessions = WebSocketSessions.getAllSession();

            TransportData td = new TransportData();
            td.setType(type);
            td.setData(data);

            ObjectMapper mapper = new ObjectMapper();
            TextMessage message = new TextMessage(mapper.writeValueAsString(td));

            List<WebSocketSessionEx> removedSessions = new LinkedList<WebSocketSessionEx>();

            for (WebSocketSessionEx webSocketSession : sessions) {
                if (webSocketSession.getSession().isOpen()) {
                    webSocketSession.getSession().sendMessage(message);
                } else {
                    removedSessions.add(webSocketSession);
                }
            }

            for (WebSocketSessionEx webSocketSession : removedSessions) {
                WebSocketSessions.removeSession(webSocketSession.getSession().getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 广播给指定公司的管理员
     *
     * @param type
     * @param data
     * @param companyID
     * @param mainCompanyID
     * @throws IOException
     */
    public static void boardcastToCompany(JSONObject data, Long companyID, Long mainCompanyID) {
        try {
            Collection<WebSocketSessionEx> sessions = WebSocketSessions.getAllSession();

            TextMessage message = new TextMessage(data.toString());

            List<WebSocketSessionEx> removedSessions = new LinkedList<WebSocketSessionEx>();

            for (WebSocketSessionEx webSocketSession : sessions) {
                if (webSocketSession.getSession().isOpen()) {
                    if ((webSocketSession.getCompanyID().equals(companyID) || webSocketSession
                        .getCompanyID().equals(mainCompanyID))) {
                        webSocketSession.getSession().sendMessage(message);
                    }
                } else {
                    removedSessions.add(webSocketSession);
                }
            }

            for (WebSocketSessionEx webSocketSession : removedSessions) {
                WebSocketSessions.removeSession(webSocketSession.getSession().getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 广播给指定区域的管理员
     *
     * @param data
     * @param areaID
     * @throws IOException
     */
    public static void boardcastToArea(JSONObject data, String cityName, Long mainCompanyID) {

        try {
            Collection<WebSocketSessionEx> sessions = WebSocketSessions.getAllSession();

            TextMessage message = new TextMessage(data.toString());

            List<WebSocketSessionEx> removedSessions = new LinkedList<WebSocketSessionEx>();

            for (WebSocketSessionEx webSocketSession : sessions) {
                if (webSocketSession.getSession().isOpen()) {
                    if ((webSocketSession.getCityName().equals(cityName) || webSocketSession
                        .getCompanyID().equals(mainCompanyID))) {
                        webSocketSession.getSession().sendMessage(message);
                    }
                } else {
                    removedSessions.add(webSocketSession);
                }
            }

            for (WebSocketSessionEx webSocketSession : removedSessions) {
                WebSocketSessions.removeSession(webSocketSession.getSession().getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
